import * as TYPES from '../action-types'
import { getSliders, getLessons } from '@/api/home'
import { LessonResult } from '@/typings/lesson'
export default {
    setCurrentCategory(currentCategory: string) {
        return { type: TYPES.SET_CURRENT_CATEGORY, payload: currentCategory }
    },
    getSliders() {
        return {
            type: TYPES.GET_SLIDERS,
            payload: getSliders()
        }
    },
    getLessons() {
        return (dispatch: any, getState: any) => {
            (async function () {
                let {
                    currentCategory,
                    lessons: { hasMore, offset, limit, loading }
                } = getState().home
                if (hasMore && !loading) {
                    dispatch({ type: TYPES.SET_LESSONS_LOADING, payload: true })
                    let result: LessonResult = await getLessons({ category: currentCategory, offset, limit })
                    dispatch({ type: TYPES.SET_LESSONS, payload: result.data })
                }
            })()
        }
    },
    refreshLessons() {
        return (dispatch: any, getState: any) => {
            (async function () {
                let {
                    currentCategory,
                    lessons: { limit, loading }
                } = getState().home
                if (!loading) {
                    dispatch({ type: TYPES.SET_LESSONS_LOADING, payload: true })
                    let result: LessonResult = await getLessons({ category: currentCategory, offset: 0, limit })
                    dispatch({ type: TYPES.REFRESH_LESSONS, payload: result.data })
                }
            })()
        }
    }
}